//package com.example.app.adapter;
//
//import android.content.Context;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.TextView;
//
//import androidx.annotation.NonNull;
//import androidx.recyclerview.widget.RecyclerView;
//
//import com.example.app.R;
//import com.example.app.entity.Reservation;
//
//import java.util.List;
//
//public class BillAdapter extends RecyclerView.Adapter<BillAdapter.BillViewHolder> {
//
//    private Context context;
//    private List<Reservation> reservationList;
//
//    public BillAdapter(Context context, List<Reservation> reservationList) {
//        this.context = context;
//        this.reservationList = reservationList;
//    }
//
//    @NonNull
//    @Override
//    public BillViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(context).inflate(R.layout.bill_item, parent, false);
//        return new BillViewHolder(view);
//    }
//
//    @Override
//    public void onBindViewHolder(@NonNull BillViewHolder holder, int position) {
//        Reservation reservation = reservationList.get(position);
//
//        holder.plateNumber.setText(reservation.getPlateNumber());
//        holder.slotNumber.setText(reservation.getPlateParkId());
//        holder.startTime.setText(reservation.getStart_time());
//        holder.endTime.setText(reservation.getEnd_time());
//        holder.price.setText("¥" + reservation.getPrice());
//    }
//
//    @Override
//    public int getItemCount() {
//        return reservationList.size();
//    }
//
//    public static class BillViewHolder extends RecyclerView.ViewHolder {
//        TextView plateNumber, slotNumber, startTime, endTime, price;
//
//        public BillViewHolder(@NonNull View itemView) {
//            super(itemView);
//            plateNumber = itemView.findViewById(R.id.plateNumber);
//            slotNumber = itemView.findViewById(R.id.slotNumber);
//            startTime = itemView.findViewById(R.id.startTime);
//            endTime = itemView.findViewById(R.id.endTime);
//            price = itemView.findViewById(R.id.price);
//        }
//    }
//}
package com.example.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.app.R;
import com.example.app.entity.Reservation;

import java.util.List;

public class BillAdapter extends RecyclerView.Adapter<BillAdapter.BillViewHolder> {

    private Context context;
    private List<Reservation> reservationList;
    private OnItemClickListener listener;

    // 定义点击回调接口
    public interface OnItemClickListener {
        void onEndOrderClick(int position);
    }

    // 设置监听器的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public BillAdapter(Context context, List<Reservation> reservationList) {
        this.context = context;
        this.reservationList = reservationList;
    }

    @NonNull
    @Override
    public BillViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.bill_item, parent, false);
        return new BillViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BillViewHolder holder, int position) {
        Reservation reservation = reservationList.get(position);

        holder.plateNumber.setText(reservation.getPlateNumber());
        holder.slotNumber.setText(reservation.getPlateParkId());
        holder.startTime.setText(reservation.getStart_time());
        holder.endTime.setText(reservation.getEnd_time());
        holder.price.setText("¥" + reservation.getPrice());

        // 设置按钮点击事件
        holder.endOrderButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int adapterPosition = holder.getAdapterPosition();
                    if (adapterPosition != RecyclerView.NO_POSITION) {
                        listener.onEndOrderClick(adapterPosition);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return reservationList.size();
    }

    public static class BillViewHolder extends RecyclerView.ViewHolder {
        TextView plateNumber, slotNumber, startTime, endTime, price;
        Button endOrderButton;

        public BillViewHolder(@NonNull View itemView) {
            super(itemView);
            plateNumber = itemView.findViewById(R.id.plateNumber);
            slotNumber = itemView.findViewById(R.id.slotNumber);
            startTime = itemView.findViewById(R.id.startTime);
            endTime = itemView.findViewById(R.id.endTime);
            price = itemView.findViewById(R.id.price);
            endOrderButton = itemView.findViewById(R.id.endOrderButton);
        }
    }
}